package example

import (
	"jslog/system/config"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

func Gorm(cfgMysql config.Server) *gorm.DB {
	cfg := cfgMysql.Mysql
	db, err := gorm.Open(mysql.New(mysql.Config{
		DSN:                       cfg.Username + ":" + cfg.Password + "@tcp(" + cfg.Path + ":" + cfg.Port + ")/" + cfg.Dbname + "?" + cfg.Config,
		DefaultStringSize:         256,
		DisableDatetimePrecision:  true,
		DontSupportRenameIndex:    false,
		DontSupportRenameColumn:   false,
		SkipInitializeWithVersion: false,
	}), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
			TablePrefix:   cfg.Prefix,
		},
		Logger: logger.Default.LogMode(logger.Silent),
	})
	if err != nil {
		panic(err.Error())
	}
	return db
}
